package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShopingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //先查询自己购物车的数据
        ShoppingCart shoppingCart = new ShoppingCart();
        //对象属性拷贝
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);

        //从ThreadLocal中获取用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);

        //执行查询操作
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        //判断该商品是否已经在购物车出现过
        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            //若商品出现过，则update数量+1
            ShoppingCart shoppingCart1 = shoppingCartList.get(0);
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
            shoppingCartMapper.updateNumById(shoppingCart1);
        } else { //若没有出现，则执行insert语句

            //判断其是插入菜品还是套餐
            Long dishId = shoppingCart.getDishId();
            if (dishId != null) {//添加的是菜品数据
                //先属性拷贝，查询对应菜品冗余字段
                Dish dish = dishMapper.getById(dishId);//根据菜品id查询菜品
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setName(dish.getName());
            } else {
                //添加的是套餐数据
                Setmeal setmeal = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setName(setmeal.getName());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //执行insert语句
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     *
     * @return
     */
    public List<ShoppingCart> showShopoingCart() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * 清空购物车
     */
    public void cleanShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }

    /**
     * 减少购物车
     *
     * @param shoppingCartDTO
     */
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //属性拷贝为ShoppingCart
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //设置userid属性
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //1.从数据库中查找相应数据获得其num
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //判断该条数据是否存在数据
        if (list != null && list.size() > 0) {
            //若已经存在数据
            ShoppingCart shoppingCart1 = list.get(0);
            //判断其份数是否等于1
            if (shoppingCart1.getNumber() == 1) {
                //若等于1,直接执行delete操作
                shoppingCartMapper.deleteById(shoppingCart1.getId());
            } else {
                //若不为1，则执行update Num-1操作
                shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
                shoppingCartMapper.updateNumById(shoppingCart1);
            }
        }
    }
}
